namespace TreeText01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            comboBox1.SelectedIndex = comboBox1.Items.IndexOf("10");
            comboBox2.SelectedIndex = comboBox2.Items.IndexOf("100");
            comboBox3.SelectedIndex = comboBox3.Items.IndexOf("0.6");
            comboBox4.SelectedIndex = comboBox4.Items.IndexOf("0.7");
            comboBox5.SelectedIndex = comboBox5.Items.IndexOf("30");
            comboBox6.SelectedIndex = comboBox6.Items.IndexOf("20");
            comboBox7.SelectedIndex = comboBox7.Items.IndexOf("Blue");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            if (graphics == null) graphics = this.CreateGraphics();
            Graphics g = this.CreateGraphics();
            g.Clear(Color.White);
            g.Dispose();
            int deep = int.Parse(comboBox1.SelectedItem.ToString()); 
            int length = int.Parse(comboBox2.SelectedItem.ToString());
            per1 = double.Parse(comboBox3.SelectedItem.ToString());
            per2 = double.Parse(comboBox4.SelectedItem.ToString());
            th1 = double.Parse(comboBox5.SelectedItem.ToString()) * Math.PI / 180;
            th2 = double.Parse(comboBox6.SelectedItem.ToString()) * Math.PI / 180;

            drawCayleyTree(deep, 200, 310, length, -Math.PI / 2);
            
            
            
        }
        private Graphics graphics;
        double th1 = 30;
        double th2 = 20;
        double per1 = 0.6;
        double per2 = 0.7;

        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {   
            string s = comboBox7.SelectedItem.ToString();
            switch (s) {
                case "Blue":
                    graphics.DrawLine(
                        Pens.Blue,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Red":
                    graphics.DrawLine(
                        Pens.Red,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Green":
                    graphics.DrawLine(
                        Pens.Green,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Black":
                    graphics.DrawLine(
                        Pens.Black,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Yellow":
                    graphics.DrawLine(
                        Pens.Yellow,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                case "Pink":
                    graphics.DrawLine(
                        Pens.Pink,
                        (int)x0, (int)y0, (int)x1, (int)y1);
                    break;
                
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        
    }
}